package com.situ.springboot.test;

import org.junit.jupiter.api.Test;

import java.util.*;

public class CollectionsDemo {

    @Test
    public void test1() {
        Collection<String> collection = new ArrayList<>();
        collection.add("张三");
        collection.add("李四");
        collection.add("王五");
        System.out.println(collection);
        collection.remove("张三");
        System.out.println(collection);
        Object[] array = collection.toArray();
        for (Object o : array) {
            System.out.println(o);
        }

        System.out.println("-----------------");
        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()) {
            String data = iterator.next();
            System.out.println(data);
        }
    }

    @Test
    public void test2() {
        List<String> list = new ArrayList<String>();
        list.add("abc1");
        list.add("abc2");
        list.add("abc3");
        list.add("abc4");
        // list.add(2, "Java");
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String data = iterator.next();
            System.out.println(data);
        }
        System.out.println("------------");
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String data = it.next();
            System.out.println(data);
            if (data.equals("abc4")) {
                // list.add("ABC3");
                // list.remove(data);
                it.remove();
            }
        }

        /*for (String s : list) {
            if (s.equals("abc4")) {
                list.remove(s);
            }
        }*/


    }

    @Test
    public void test3() {
        Vector<String> vector = new Vector<>();
        vector.add("Java");
        vector.add("C++");
        vector.add("Python");

    }

    @Test
    public void test4() {
        Student student1 = new Student(1, "zhangsan", 26, "男");
        Student student2 = new Student(1, "zhangsan", 23, "男");
        Set<Student> set = new HashSet<>();
        set.add(student1);
        set.add(student2);
        for (Student stu : set) {
            System.out.println(stu);
        }
        System.out.println("----------------");
        /*Iterator<Student> iterator = set.iterator();
        while (iterator.hasNext()) {
            Student stu = iterator.next();
            System.out.println(stu);
        }*/
    }

    @Test
    public void test51() {
        Student student1 = new Student(1, "zhangsan", 26, "男");
        Student student2 = new Student(1, "zhangsan", 23, "男");
        Map<String, Student> map = new HashMap<>();
        map.put("stu", student1);
        map.put("stu", student2);
        System.out.println(map);
    }

    @Test
    public void test55() {
        System.out.println("重地".hashCode());
        System.out.println("通话".hashCode());
        System.out.println("abc".hashCode());
    }

    @Test
    public void test551() {
        // 创建HashSet对象
        Set<String> set = new HashSet<String>();
        // 给集合中添加自定义对象
        set.add("zhangsan");
        set.add("lisi");
        set.add("wangwu");
        set.add("zhangsan");
        set.add(new String("lisi"));
        for (String string : set) {
            System.out.println(string);
        }
    }

    @Test
    public void test44() {
        Set<String> set = new LinkedHashSet<String>();
        set.add("bbb");
        set.add("aaa");
        set.add("abc");
        set.add("bbc");
        Iterator it = set.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }

    @Test
    public void test5() {
        TreeSet<Student> treeSet = new TreeSet<>();
        treeSet.add(new Student(1, "张三", 23, "男"));
        treeSet.add(new Student(2, "李四", 13, "男"));
        treeSet.add(new Student(3, "周七", 14, "男"));
        treeSet.add(new Student(4, "王五", 43, "男"));
        treeSet.add(new Student(5, "赵六", 33, "男"));

        System.out.println(treeSet);
    }

    @Test
    public void test133() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("邓超", "孙俪");
        map.put("李晨", "范冰冰");
        map.put("刘德华", "柳岩");
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            System.out.println(entry.getKey() + " :  " + entry.getValue());
        }
        System.out.println("----------");
        Iterator<Map.Entry<String, String>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            System.out.println(entry.getKey() + " :  " + entry.getValue());
        }
    }
}
